﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using car.Models;
using System.Linq;
using System;

namespace car.Controllers
{
    public class OrderController : Controller
    {
        public IActionResult Index()
        {
            try
            {
                using (var db=new carContext())
                {
                    var order = db.Order.ToList();
                    ViewBag.Orderid=db.Order.Count();
                    ViewBag.Order = true;
                    return View(order);
                }

            }
            catch
            {
                ViewBag.Order = false;
                ViewBag.Orderid = 0;
                return View(null);
            }
            
        }
        public IActionResult Delete(string id)
        {
            Order order;
            //string id = Request.Form["id"];
            //user.Id = id;
            try
            {

                using (var db = new carContext())
                {
                    order = db.Order.Find(id);
                    if (order != null)
                    {
                        db.Order.Remove(order);
                        ViewData["Flag"] = true;
                    }
                    else
                    {
                        ViewData["Flag"] = false;
                    }
                    db.SaveChanges();
                    return RedirectToAction("Index", db.Order.ToList());
                }
            }
            catch
            {

                ViewData["Flag"] = false;
            }
            return View("Index");
        }
        public IActionResult Update(string id)
        {
            ViewData.Clear();
            ViewData["Oid"] = id;
            return View();
        }
        public IActionResult Updated(string id)
        {
            //string id = Request.Form["oid"];
            try
            {
                using (var db = new carContext())
                {
                    var order = db.Order.Find(id);
                    if (order == null)
                    {
                        order = new Order();
                        order.Oid = id;
                        
                    }
                    order.Tid = Request.Form["tid"];
                    order.Uid = Request.Form["uid"];
                    db.Order.Update(order);
                    ViewBag.Order = true;
                    db.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                ViewBag.Order = false;
            }
            return View("Update");
        }
        public IActionResult Create()
        {
            return View();
        }
        public IActionResult Created()
        {
            string oid = Request.Form["oid"];
            try
            {
                using(var db=new carContext())
                {
                    var order = db.Order.Find(oid);
                    if(order == null)
                    {
                        order = new Order();
                        order.Oid = oid;
                        order.Tid = Request.Form["tid"];
                        order.Uid = Request.Form["uid"];
                        db.Order.Add(order);
                        ViewBag.Order = true;
                        db.SaveChanges();
                    }
                    else
                    {
                        ViewBag.Order = false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
                ViewBag.Order = false;
            }
            return View("Create");
        }
        public IActionResult UserIndex()
        {
            try
            {
                ViewData["uid"] = HomeController.UserUid;
                using (var db = new carContext())
                {
                    var order = db.Order.ToList();
                    ViewBag.Orderid = db.Order.Count();
                    ViewBag.Order = true;
                    return View(order);
                }

            }
            catch
            {
                ViewBag.Order = false;
                ViewBag.Orderid = 0;
                return View(null);
            }

        }
        [HttpPost]
        public IActionResult SearchID(string name)
        {
            var subscribelist = new carContext();
            var filelist = subscribelist.Order.ToList();
            if (!string.IsNullOrEmpty(name))
                filelist = filelist.Where(p => p.Uid.Contains(name)).ToList();
            return View("Index", filelist);
        }
    }
}
